var request = {
	post: function(url, params = {}, success, error) {
		this.requestBase(url, "post", params, success, error)
	},

	get: function(url, params = {}, success, error) {
		this.requestBase(url, "get", params, success, error)
	},

	put: function(url, params = {}, success, error) {
		this.requestBase(url, "put", params, success, error)
	},

	delete: function(url, params = {}, success, error) {
		this.requestBase(url, "delete", params, success, error)
	},

	requestBase: function(url, type, params = {}, success, error) {
		// let baseUrl = 'http://192.168.0.101:5502' 
		let baseUrl = localStorage.getItem("ip");
		if (baseUrl == '') {
			mui.toast("请设置服务地址");
			return
		}
		baseUrl = baseUrl.indexOf('http://') == 0 ? baseUrl : 'http://' + baseUrl;
		url = baseUrl + url;
		var headers = {
			'Content-Type': 'application/json'
		};

		var token = localStorage.getItem("token");
		if (token) {
			headers['authorization'] = token;
		}
		let data = ''
		if (params) {
			var strP = Object.keys(params).filter(key => params[key] != null ? true : false).map(key => {
				let value = params[key];
				if (typeof value === 'object') {
					let obj = '';
					for (const prop of Object.keys(value)) {
						let params = key + '[' + prop + ']';
						var subPart = encodeURIComponent(params) + "=";
						obj += subPart + encodeURIComponent(value[prop])+"&";
					}
					obj = obj.slice(0, -1);
					// console.log('对象参数',decodeURIComponent(obj));
					return obj;
				} else {
					return key + '=' + encodeURIComponent(value);
				}
			}).join('&');
			if ("get" == type) {
				data = strP;
				// console.log('提交参数',data)
			} else {
				data = params;
				// console.log('提交参数',JSON.stringify(data))
			}
		}

		var opt = Object.assign({
			data: data
		}, {
			dataType: 'json', //服务器返回json格式数据
			type: type, //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			headers: headers,
			success: function(data) {
				if (data.code === 200) {
					if (typeof success === 'function') {
						// console.log('调用API成功', data)
						success(data)
					}
				} else if (data.code === 401) {
					console.log('登陆状态过期')
					mui.toast("登陆状态过期,请重新登陆")
					
					// var wvs = plus.webview.all();
					//登录失效关闭所有页面
					// for (var i = 0, len = wvs.length; i < len; i++) {
					// 	plus.webview.close(wvs[i]);
					// }
					//跳转登录页面
					plus.webview.open('login.html');
					//清空缓存
					localStorage.setItem("isLogin", false);
					localStorage.setItem("token", "");
					localStorage.setItem("userId", "");
					localStorage.setItem("user", "");
					
					var curr = plus.webview.currentWebview();
					curr.close();
				} else {
					console.error('调用API发生业务异常', data)
					mui.toast("系统内部错误：" + data.msg);
					if (typeof error === 'function') {
						error("系统内部错误", data)
					}
				}
			},
			error: function(xhr, type, errorThrown) {
				console.error('调用API发生系统异常', type, errorThrown)
				mui.toast("系统内部错误");
				if (typeof error === 'function') {
					error(type, errorThrown)
				}
			}
		});
		// console.log('调用服务器API：', url)
		mui.ajax(url, opt);
	}
}